
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/mvp/base_page_presenter.dart';
import 'package:flutter_deer/net/base_entity.dart';
import 'package:flutter_deer/net/net.dart';
import 'package:flutter_deer/widgets/iview/file_upload_iview.dart';

class FileUploadPagePresenter extends BasePagePresenter<FileUploadIMvpView> {

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {});
  }

  Future<dynamic> uploadWebFile(MultipartFile file) async {
    try{
      final FormData formData = FormData.fromMap(<String, dynamic>{
        'file': file
      });
      await requestNetwork<BaseEntity<FileUploadEntity>>(Method.post,
          url: HttpApi.upload,
          params: formData,
          onSuccess: (data) {
            view.uploadResult(data!.data);
          }
      );
    } catch(e) {
      view.showToast('上传失败！');
    }
  }

Future<dynamic> uploadFile(String path,String mimeType) async {
  try{
    // final String path = image.path;
    final String name = path.substring(path.lastIndexOf('/') + 1);
    final FormData formData = FormData.fromMap(<String, dynamic>{
      'file': await MultipartFile.fromFile(path, filename: name)
    });
    await requestNetwork<BaseEntity<FileUploadEntity>>(Method.post,
        url: HttpApi.upload,
        params: formData,
        onSuccess: (data) {
          view.uploadResult(data!.data);
        }
    );
  } catch(e) {
    view.showToast('上传失败！');
  }
}

}